from time import time as _time
from random import choice as _choice
# from multiprocessing import Pool as _Pool
from pyperclip import paste, copy
__all__ = [
    "typeof",
    "tryget",
    "fillist",
    "rechoice",
    "frange",
    "join",
    "split",
    "presskey",
    "dowith", "dountil", "dolist",
    "Timer",
    "reSlice",
    #"multprocs",
    "ctrlC", "ctrlV",
]


def typeof(obj: object) -> str:
    """获取一个实例所对应的类名
    obj:任意类的实例\n
    返回类名的字符串
    """
    return obj.__class__.__name__

def tryget(lst: list, i: int):
    """尝试获取列表元素"""
    return lst[i] if i in range(len(lst)) else 0


def fillist(valu, spac, lst) -> list:
    """把lst中的spac替换成valu"""
    return [valu if x == spac else x for x in lst]


def rechoice(lst: list, num: int = 1) -> list:
    """多次重复实验"""
    return [_choice(lst) for _ in range(num)]


def frange(a: float, b: float = None, c: float = 1):
    """浮点数的range函数"""
    if b is None:
        a, b = 0, a
    while (b-a)*c > 0:
        yield a
        a += c


def join(lst: list) -> str:
    """join的列表版本"""
    return ''.join(lst)


def split(lst: list, pice: int) -> list:
    """将列表分割成pice份"""
    l = int(len(lst)/pice)
    return [lst[i*l:i*l+l] for i in range(pice)]


def presskey(*lst) -> None:
    """模拟按键"""
    try:
        from pyautogui import hotkey as _hotkey
        _hotkey(*lst)
    except:
        print("need install pyautogui.")


def dowith(func, *n):
    o = func(*n)
    return (func if o == None else o, *n)


def dountil(valu, func, *n):
    o = list(n)
    while not valu(*o):
        o = list(func(*o))
    return o


def dolist(func, lst):
    if len(lst) == 0:
        return
    o = lst[0]
    for i in range(1, len(lst)):
        o = func(o, lst[i])
    return o


# def multprocs(num: int, func, inlist: list, iter: bool = False) -> list:
#     """多进程计算，num进程数，func计算函数，inlist输入列表，iter是否迭代"""
#     pol = _Pool(num)
#     if iter:
#         return pol.imap(func, inlist)
#     else:
#         return pol.map(func, inlist)


class Timer:
    """计时器类"""

    def __init__(self):
        self.createtime: float = _time()
        self.startdict: dict[str, float] = {}
        self.stopdict: dict[str, float] = {}
        self.keylist: list[str] = []

    def __len__(self):
        return len(self.keylist)

    def start(self, key: str = '') -> None:
        key = repr(key)
        self.keylist.append(key)
        self.startdict[key] = _time()

    def stop(self, key: str = '') -> None:
        key = repr(key)
        self.stopdict[key] = _time()

    def __iter__(self):
        for key in self.keylist:
            yield self[key]

    def __getitem__(self, key: str):
        key = repr(key)
        return self.startdict.get(key, 0), self.stopdict.get(key, 0)


def reSlice(sli: slice, leng: int, bottom: int = 0) -> slice:
    """处理切片的省略"""
    if leng is None:
        assert sli.stop is not None
    leng, bottom = int(leng), int(bottom)
    mi, ma = min(bottom, bottom+leng), max(bottom, bottom+leng)
    start = bottom if sli.start is None else sli.start
    start = start if mi < start < ma else bottom
    stopt = bottom+leng if sli.stop is None else sli.stop
    stopt = stopt if mi < stopt < ma else bottom+leng
    stept = 1 if sli.step is None else sli.step
    return slice(start, stopt, stept)


def ctrlC(text: str) -> None:
    copy(text)


def ctrlV() -> str:
    return paste()
